package com.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IExaminationService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

/**
 * @author wufeng
 * @date 创建时间：2016年8月19日 下午2:10:49 
 */
@Controller
@RequestMapping("exam")
public class ExaminationController {
	@Autowired
	private IExaminationService examinationService;
	
	/**
	* 前台考试页面
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月20日 上午9:57:14 
	* @return  
	*/
	@RequestMapping("mv")
	public ModelAndView mv(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("examination/examination");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("user", user);
		return mv;
	}
	
	/**
	* 试卷列表数据
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月20日 上午9:57:31 
	* @return  
	*/
	@RequestMapping("list")
	public void list(HttpServletRequest request, 
			HttpServletResponse response){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		String jsonsString  = request.getParameter("data");
		Map<String, Object> map = JSONSerielizer.toMap(jsonsString);
		Map<String, Object> remap = examinationService.getMapList(user,map);
		String json = JSONSerielizer.toJson(remap, null, null);
		OutputUtil.returnString(response, json);
	}
	
	/**
	* 试卷页面
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月20日 上午10:22:10 
	* @return  
	*/
	@RequestMapping("examMv/{id}/{examId}/{type}")
	public ModelAndView examMv(HttpServletRequest request, @PathVariable Integer id, @PathVariable Integer examId,@PathVariable String type){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("examination/exam");
		if(id==0){
			Map<String, Object> reMap= examinationService.addPaper(user, examId);
			mv.addObject("id",reMap.get("oid"));
			examinationService.updateStartTime(reMap.get("oid").toString());
			
		}else{
			mv.addObject("id",id);
			examinationService.updateStartTime(id.toString());
		}
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("examId",examId);
		mv.addObject("type",type);
		return mv;
	}
	
	@RequestMapping("submit/{id}")
	public void submit(HttpServletRequest request, 
			HttpServletResponse response, @PathVariable String id){
		String json = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = examinationService.submit(id, map, user);
		OutputUtil.returnSuccess(response, isSuccess);
	
	}
	@RequestMapping("paperInfo/{id}/{examId}")
	public ModelAndView infoMv(@PathVariable Integer id, @PathVariable Integer examId){
		ModelAndView mv = new ModelAndView("examination/examInfo");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("id", id);
		mv.addObject("examId",examId);
		return mv;
		
	}
	@RequestMapping("infoData/{id}")
	public void getInfodata(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer id){
		Map<String, Object> map =examinationService.getMap(id);
		String json = JSONSerielizer.toJson(map, null, null);
		OutputUtil.returnString(response, json);
	}
	
}
